package main.Daoimpl;

/**
 * 作者: ALIM-MASTIK
 * 创建实现: 2023/7/14 21:39
 * 项目名称：chat
 * 软件: IntelliJ IDEA
 */
import main.JdbcUtils;
import main.dao.UserDAO;
import main.pojo.User;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class UserDAOImpl implements UserDAO {
    private Connection connection= JdbcUtils.getConnection();
    private QueryRunner queryRunner=new QueryRunner();

    // 构造方法

    public UserDAOImpl(Connection connection) {
        this.connection = connection;
        this.queryRunner = new QueryRunner();
    }
    public UserDAOImpl(){}

    // 实现接口方法
    @Override
    public User getUserByID(int userID)  {
        String query = "SELECT * FROM users WHERE UserID = ?";
        try {
            return queryRunner.query(connection, query, new BeanHandler<>(User.class), userID);
        } catch (SQLException e) {
            return null;
        }
    }
    @Override
    public List<User> getAllUsers()  {
        String query = "SELECT * FROM users";
        try {
            return queryRunner.query(connection, query, new BeanListHandler<>(User.class));
        } catch (SQLException e) {
            return null;
        }
    }

    @Override
    public Boolean createUser(User user)  {
        String query = "INSERT INTO users (UserID, Username, Password, Nickname, Avatar) VALUES (?, ?, ?, ?, ?)";
        try {
            queryRunner.update(connection, query, user.getUserID(), user.getUsername(), user.getPassword(),
                    user.getNickname(), user.getAvatar());
            return true;
        } catch (SQLException e) {
            return false;
        }
    }
    @Override
    public Boolean updateUser(User user)  {
        String query = "UPDATE users SET Username = ?, Password = ?, Nickname = ?, Avatar = ? WHERE UserID = ?";
        try {
            queryRunner.update(connection, query, user.getUsername(), user.getPassword(), user.getNickname(),
                    user.getAvatar(), user.getUserID());
            return true;
        } catch (SQLException e) {
            return false;
        }
    }
    @Override
    public Boolean deleteUser(int userID)  {
        String query = "DELETE FROM users WHERE UserID = ?";
        try {
            queryRunner.update(connection, query, userID);
            return true;
        } catch (SQLException e) {
            return false;
        }
    }
    @Override
    public User getUserByUsername(String username)  {
        String query = "SELECT * FROM users WHERE Username = ?";
        User user =new User();
        try {
            user = queryRunner.query(connection, query, new BeanHandler<>(User.class), username);
            return user;
        } catch (SQLException e) {
            return null;
        }
    }

    @Override
    public User getUserByUserNameAndPassword(String username, String Password) {
        String query = "SELECT * FROM users WHERE Username = ? and password=?";
        try {
            User user  = queryRunner.query(connection, query, new BeanHandler<>(User.class), username,Password);
            return user;
        } catch (SQLException e) {
            return null;
        }
    }

    // 关闭数据库连接
    public void closeConnection() {
        DbUtils.closeQuietly(connection);
    }
}
